<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateLabelRequest;
use App\Http\Requests\UpdateLabelRequest;
use App\Repositories\LabelRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class LabelController extends AppBaseController
{
    /** @var  LabelRepository */
    private $labelRepository;
    private $pic;

    public function __construct(LabelRepository $labelRepo, PicturesRepository $picturesRepository)
    {
        $this->labelRepository = $labelRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the Label.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->labelRepository->pushCriteria(new RequestCriteria($request));
        $labels = $this->labelRepository->paginate(20);

        return view('labels.index')
            ->with('labels', $labels);
    }

    /**
     * Show the form for creating a new Label.
     *
     * @return Response
     */
    public function create()
    {
        return view('labels.create');
    }

    /**
     * Store a newly created Label in storage.
     *
     * @param CreateLabelRequest $request
     *
     * @return Response
     */
    public function store(CreateLabelRequest $request)
    {
        $input = $request->all();
        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $this->pic->uploadByHash($_image,"colors",null,null);
            $input['pic'] = $_res->url;
        }else{
            unset($input['pic']);
        };

        $label = $this->labelRepository->create($input);

        Flash::success('Label saved successfully.');

        return redirect(route('labels.index'));
    }

    /**
     * Display the specified Label.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $label = $this->labelRepository->findWithoutFail($id);

        if (empty($label)) {
            Flash::error('Label not found');

            return redirect(route('labels.index'));
        }

        return view('labels.show')->with('label', $label);
    }

    /**
     * Show the form for editing the specified Label.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $label = $this->labelRepository->findWithoutFail($id);

        if (empty($label)) {
            Flash::error('Label not found');

            return redirect(route('labels.index'));
        }

        return view('labels.edit')->with('label', $label);
    }

    /**
     * Update the specified Label in storage.
     *
     * @param  int              $id
     * @param UpdateLabelRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateLabelRequest $request)
    {
        $label = $this->labelRepository->findWithoutFail($id);

        if (empty($label)) {
            Flash::error('Label not found');

            return redirect(route('labels.index'));
        }
        $input = $request->all();
        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $this->pic->uploadByHash($_image,"colors",null,null);
            $input['pic'] = $_res->url;
        }else{
            unset($input['pic']);
        };

        $label = $this->labelRepository->update($input, $id);

        Flash::success('Label updated successfully.');

        return redirect(route('labels.index'));
    }

    /**
     * Remove the specified Label from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $label = $this->labelRepository->findWithoutFail($id);

        if (empty($label)) {
            Flash::error('Label not found');

            return redirect(route('labels.index'));
        }

        $this->labelRepository->delete($id);

        Flash::success('Label deleted successfully.');

        return redirect(route('labels.index'));
    }
}
